from django.db import models


class City(models.Model):
    name = models.CharField(max_length=100, unique=True, verbose_name="城市名称")
    create_at = models.DateTimeField(auto_now_add=True)
    update_at = models.DateTimeField(auto_now=True)

    class Meta:
        verbose_name = "城市"
        verbose_name_plural = "城市列表"


class IDC(models.Model):
    name = models.CharField(max_length=100, verbose_name="机房名称")
    city = models.ForeignKey(City, on_delete=models.CASCADE, related_name="idcs", verbose_name="所属城市")
    address = models.CharField(max_length=255, null=True, blank=True, verbose_name="地址")
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    class Meta:
        verbose_name = "机房"
        verbose_name_plural = "机房"
        unique_together = ("name", "city")


class Host(models.Model):
    hostname = models.CharField(max_length=100, verbose_name="主机名")
    ip = models.GenericIPAddressField(unique=True, verbose_name="IP地址")
    idc = models.ForeignKey(IDC, on_delete=models.CASCADE, related_name="hosts", verbose_name="所属机房")
    root_password = models.CharField(max_length=255, verbose_name="加密的root密码")  # 存储加密后密码
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    class Meta:
        verbose_name = "主机"
        verbose_name_plural = "主机"


class HostStatistic(models.Model):
    city = models.ForeignKey(City, on_delete=models.CASCADE, related_name="statistics", verbose_name="城市")
    idc = models.ForeignKey(IDC, on_delete=models.CASCADE, related_name="statistics", verbose_name="机房")
    date = models.DateField(verbose_name="统计日期")
    count = models.IntegerField(verbose_name="主机数量")
    created_at = models.DateTimeField(auto_now_add=True)

    class Meta:
        verbose_name = "主机统计"
        verbose_name_plural = "主机统计"
        unique_together = ("city", "idc", "date")  # 每日唯一统计